Hi Todd,
One common and classic programming technique is called a "State Machine" that can be used to control multiple concurrent sequences with only one CPU thread. The fundamental idea is that each independent task must remember its current "state" so that the code can exit, go off and do other things, then come back and return to what it was doing.
The first step in using the method is to define "states" for each task. A state is defined for each mode that might persist for a "long" time. This is normally when waiting for a timer or something external to happen. Things that can be performed instantaneously do not need separate states. If a state decides that it is time to switch to a different state, it just changes the state variable to the new state.
I like to use the structure shown below because it separates all the code and data for each task and is fairly readable and is expandable to any number of tasks and any number of states in each task.
If you aren't familiar with a C Language "switch" statement it can be used instead of number of if (State==XXX) statements.
Regards
TK
#include "KMotionDef.h"
void ServicePrintRate(); void ServiceTask2(); void ServiceTask3(); void ServiceTask4();
main() { for (;;) { WaitNextTimeSlice(); ServicePrintRate(); ServiceTask2(); ServiceTask3(); ServiceTask4(); } }
#define RATE_INIT 0 #define RATE_DELAY 1
int RateState=RATE_INIT; double t0,t1,new,old;
void ServicePrintRate() { switch (RateState) { case RATE_INIT: // setup to begin a measurement old = ch4->Dest; t0=Time_sec(); RateState=RATE_DELAY; // goto delay state break; case RATE_DELAY: if (Time_sec() > t0 + 1.0) // delay finished?
{ new = ch4->Dest; // yes, finish measurement t1=Time_sec(); printf("Rate = %13.2f steps/sec\n",(new-old)/(t1-t0)); RateState=RATE_INIT; // return to initial state } break; } }
void ServiceTask2() { }
void ServiceTask3() { }
void ServiceTask4() { }
--- On Thu, 6/16/11, morgtod <todmorg@...> wrote:
From: morgtod <todmorg@...> Subject: [DynoMotion] c example To: DynoMotion@yahoogroups.com Date: Thursday, June 16, 2011, 6:38 AM
I am trying to use a couple of the example c files in a single code. How would you program a for (::) like in the external feedhold example, along with another for(::) with a delay_sec(1.0), like in the show steprate example?
I have 2 oil level switches, 2 air flow sensors and 3 proximity sensors, 1 spindle thermal switch, 1 spindle thermistor and a spindle chiller flow sensor that I need to fit into for(;;) loops, but some need delays and the delays cannot effect the other loops.
Thanks!
|